﻿Imports System.Data.SqlClient

Public Class frmKiemTraMatHangChenhLech
    'Cac bien dung de truy xuat du lieu in DatagridView
    Private dsMathang As DataSet
    Public objPhieu As CPhieuNX

    Public Sub FormatGridStyleSLNX()
        If cmbLoaiKiemTra.SelectedIndex = 0 Then
            dsMathang = BaseDB.ExecSqlProcedure_DataSet("sp_GetDSMatHangChenhLechSL")
        Else
            dsMathang = BaseDB.ExecSqlProcedure_DataSet("sp_GetDSMatHangKhongDungNgTTon")
        End If
        Dim dv As DataView
        dv = dsMathang.Tables(0).DefaultView
        gridKiemTra.DataSource = dv
        gridKiemTra.RetrieveStructure()
        gridKiemTra.GroupByBoxVisible() = False
        With gridKiemTra.RootTable
            .Groups.Add(New Janus.Windows.GridEX.GridEXGroup(gridKiemTra.RootTable.Columns("ghichu")))
            .GroupRowFormatStyle.ForeColor = Color.Red
            .SortKeys.Add(New Janus.Windows.GridEX.GridEXSortKey(gridKiemTra.RootTable.Columns("ma"), SortOrder.Ascending))

            .Columns.Item("mathang_id").Visible = False
            .Columns.Item("ghichu").Visible = False
            With .Columns.Item("ghichu")
                .Caption = "Ghi chú"
            End With
            With .Columns.Item("ma")
                .Caption = "Mã"
                .Width = 100
            End With

            With .Columns.Item("code")
                .Caption = "Code"
                .Width = 40
            End With

            With .Columns.Item("ten")
                .Caption = "Tên hàng"
                .Width = 150
            End With

            With .Columns.Item("lk")
                .Caption = "Loại kho"
                .Width = 50
            End With


            With .Columns.Item("lo")
                .Caption = "Lô hàng"
                .Width = 100
            End With

            With .Columns.Item("gle")
                .Caption = "Giá lẻ"
                .FormatString = FormatMoneyStyle()
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
                .Width = 50
            End With

            With .Columns.Item("dvt")
                .Caption = "Đvt"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .Width = 50
            End With

            With .Columns.Item("hansd")
                .Caption = "Hạn SD"
                .Width = 50
            End With

            With .Columns.Item("kho")
                .Caption = "Kho"
                .Width = 50
            End With

            With .Columns.Item("h")
                .Caption = "CCL"
                .Width = 50
            End With
        End With
    End Sub
    Private Sub frmKiemTraMatHangChenhLech_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbLoaiKiemTra.Items.Add("Mặt hàng có các số liệu không khớp")
        cmbLoaiKiemTra.Items.Add("Ngày tạo tồn của mặt hàng không đúng")
        cmbLoaiKiemTra.SelectedIndex = 0
        FormatGridStyleSLNX()
    End Sub


    Private Sub btnDieuChinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDieuChinh.Click
        Try
            If gridKiemTra.RowCount > 0 Then
                If cmbLoaiKiemTra.SelectedIndex = 0 Then
                    For Each row As Janus.Windows.GridEX.GridEXRow In gridKiemTra.GetRows
                        For Each dr As Janus.Windows.GridEX.GridEXRow In row.GetChildRows()
                            Dim par(0) As SqlClient.SqlParameter
                            par(0) = New SqlParameter("@mathang_id", SqlDbType.Decimal)
                            par(0).Value = dr.Cells("mathang_id").Value
                            BaseDB.ExecSqlProcedure_NoneQuery("sp_DieuChinhLaiMatHangChenhLechSL", par)
                        Next
                    Next
                Else
                    BaseDB.ExecSqlProcedure_NoneQuery("sp_DieuChinhLaiMatHangKhongDungNgTTon")
                End If
                ShowMessages("Điều chỉnh xong.")
            End If
        Catch ex As Exception
            ShowMessages(ex.ToString())
        End Try
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub cmbLoaiKiemTra_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLoaiKiemTra.SelectedIndexChanged
        FormatGridStyleSLNX()
    End Sub

    Private Sub gridKiemTra_RowDoubleClick(ByVal sender As Object, ByVal e As Janus.Windows.GridEX.RowActionEventArgs) Handles gridKiemTra.RowDoubleClick
        Dim frm As New frmQuanLyLoHang(e.Row.Cells("lk").Value.ToString(), e.Row.Cells("kho").Value.ToString(), CInt(e.Row.Cells("mathang_id").Value))
        frm.ShowDialog(Me)
    End Sub
End Class